<?php

namespace app\admin\service;

use app\model\ConsultOfflineModel;
use app\model\ConsultOnlineModel;
use app\model\ConsultQuestionModel;
use app\model\not_del\ConsultDelLogModel;
use app\model\UserModel;
use think\Service;

class ConsultantService extends Service
{
    /**
     * 按问题分类统计-个人
     * @param $consult_id
     * @param $unit_id
     * @return array
     */
    public function getQuestionType($consult_id, $unit_id): array
    {
        $ConsultQuestionModel = new ConsultQuestionModel();
        $ConsultOnlineModel = new ConsultOnlineModel();
        $ConsultOfflineModel = new ConsultOfflineModel();
        $question = $ConsultQuestionModel->field('id,name')->where([
            ['unit_id','=',$unit_id],
            ['status','=',1]
        ])->select()->toArray();
        $data = [];
        foreach ($question as $item) {
            $data['name'][] = $item['name'];
            $ConsultOnlineUid = $ConsultOnlineModel->where([
                ['consult_id','=',$consult_id],
                ['question_id','=',$item['id']],
            ])->distinct(true)->column('uid');
            $ConsultOfflineUid = $ConsultOfflineModel->where([
                ['consult_id','=',$consult_id],
                ['question_id','=',$item['id']],
            ])->distinct(true)->column('uid');
            $uid = array_merge($ConsultOnlineUid,$ConsultOfflineUid);
            $uid = array_unique($uid);
            $data['num'][] = count($uid);
        }
        return $data;
    }

    /**
     * 月度人数统计-个人
     * @param $consult_id
     * @param $unit_id
     * @return array
     */
    public function getOneMonthPeopleNumber($consult_id, $unit_id): array
    {
        $ConsultOnlineModel = new ConsultOnlineModel();
        $ConsultOfflineModel = new ConsultOfflineModel();
        $UserModel = new UserModel();
        $data = [];
        for ($i=1;$i<=12;$i++){
            $start_time = strtotime(date('Y-'.$i.'-01'));
            $end_time = strtotime(date('Y-'.$i.'-t 23:59:59'));
            $data['date'][] = date('Y-m',$start_time);
            $ConsultOnlineUid = $ConsultOnlineModel->where([
                ['unit_id','=',$unit_id],
                ['consult_id','=',$consult_id],
                ['time','between',[$start_time,$end_time]]
            ])->distinct(true)->column('uid');
            $ConsultOfflineUid = $ConsultOfflineModel->where([
                ['unit_id','=',$unit_id],
                ['consult_id','=',$consult_id],
                ['time','between',[$start_time,$end_time]]
            ])->distinct(true)->column('uid');
            $uid = array_merge($ConsultOnlineUid,$ConsultOfflineUid);
            $uid = array_unique($uid);
            $data['boy'][] = $UserModel->where([
                ['id','in',$uid],
                ['sex','=',1]
            ])->count('id');
            $data['girl'][] = $UserModel->where([
                ['id','in',$uid],
                ['sex','=',2]
            ])->count('id');
        }
        return $data;
    }

    /**
     * 累计接待人数统计-个人
     * @param $consult_id
     * @param $unit_id
     * @return array
     */
    public function getOneReceptionCapacity($consult_id, $unit_id): array
    {
        $UserModel = new UserModel();
        $ConsultOnlineModel = new ConsultOnlineModel();
        $ConsultOfflineModel = new ConsultOfflineModel();
        $ConsultOnlineCount = $ConsultOnlineModel->where([
            ['unit_id','=',$unit_id],
            ['consult_id','=',$consult_id],
        ])->count();
        $ConsultOfflineCount = $ConsultOfflineModel->where([
            ['unit_id','=',$unit_id],
            ['consult_id','=',$consult_id],
        ])->count();
        $ConsultOnlineUid = $ConsultOnlineModel->where([
            ['unit_id','=',$unit_id],
            ['consult_id','=',$consult_id],
        ])->distinct(true)->column('uid');
        $ConsultOfflineUid = $ConsultOfflineModel->where([
            ['unit_id','=',$unit_id],
            ['consult_id','=',$consult_id],
        ])->distinct(true)->column('uid');
        $ConsultOnlineTotal = count($ConsultOnlineUid);
        if ($ConsultOnlineTotal > 0){
            $ConsultOnlineBoy = $UserModel->where([
                ['id','in',$ConsultOnlineUid],
                ['sex','=',1]
            ])->count('id');
            $ConsultOnlineGirl = $ConsultOnlineTotal - $ConsultOnlineBoy;
        }else{
            $ConsultOnlineBoy = 0;
            $ConsultOnlineGirl = 0;
        }
        $ConsultOfflineTotal = count($ConsultOfflineUid);
        if ($ConsultOfflineTotal > 0){
            $ConsultOfflineBoy = $UserModel->where([
                ['id','in',$ConsultOfflineUid],
                ['sex','=',1]
            ])->count('id');
            $ConsultOfflineGirl = $ConsultOfflineTotal - $ConsultOfflineBoy;
        }else{
            $ConsultOfflineBoy = 0;
            $ConsultOfflineGirl = 0;
        }
        return [
            'ConsultOnlineCount' => $ConsultOnlineCount,
            'ConsultOfflineCount' => $ConsultOfflineCount,
            'ConsultOnlineBoy' => $ConsultOnlineBoy,
            'ConsultOnlineGirl' => $ConsultOnlineGirl,
            'ConsultOfflineBoy' => $ConsultOfflineBoy,
            'ConsultOfflineGirl' => $ConsultOfflineGirl
        ];
    }

    public function del($id,$type,$userData): void
    {
        $ConsultDelLogModel = new ConsultDelLogModel();
        if (is_array($id)) {
            foreach ($id as $v) {
                $dataSave[] = [
                    'uid' => $userData['id'],
                    'type' => $type,
                    'type_id' => $v,
                    'time' => time()
                ];
            }
            if (!empty($dataSave)){
                $ConsultDelLogModel->insertAll($dataSave);
            }
        } else {
            $ConsultDelLogModel->insert([
                'uid' => $userData['id'],
                'type' => $type,
                'type_id' => $id,
                'time' => time()
            ]);
        }
    }
}